//
//  LoginViewController.m
//  MyApplication
//
//  Created by 彭凯敏 on 15/8/4.
//  Copyright (c) 2015年 52xiaoluo. All rights reserved.
//

#import "LoginViewController.h"

#import "UIImage+Exit.h"


@interface LoginViewController ()
@property (strong, nonatomic)  UIImageView *backImage;

@property (strong, nonatomic)  UIView *userView;
@property (strong, nonatomic)  UIView *passView;


@property (strong, nonatomic)  UITextField *userTF;
@property (strong, nonatomic)  UITextField *passwordTF;
@property (strong, nonatomic)  UIButton *loginBtn;



@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginError:) name:@"LoginError" object:nil];
    
    [self createUI];
    
    _userTF.text = GetNSUserDefaults(@"UserName");

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;
    
    //    UITextField *textField = aNotification.object;
    
    if ([_userTF isFirstResponder]) {
        CGFloat offset = [UIViewCategory loginOffsetText:_userView Height:height Super:self.view];
        
        if (offset>0) {
            [UIView animateWithDuration:0.25 animations:^{
                self.view.originY = -offset;
            }];
        }
    }
    else if ([_passwordTF isFirstResponder])
    {
        CGFloat offset = [UIViewCategory loginOffsetText:_passView Height:height Super:self.view];
        
        if (offset>0) {
            [UIView animateWithDuration:0.25 animations:^{
                self.view.originY = -offset;
            }];
        }
    }
    
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
    if (self.view.originY !=0) {
        [UIView animateWithDuration:0.25 animations:^{
            self.view.originY = 0;
            
        }];
    }
    
}
- (void)createUI
{
    UIImageView *backView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    
    _loginBtn = [[UIButton alloc]init];
    [_loginBtn setBackgroundColor:RGB(71,133,229, 1)];
    _loginBtn.titleLabel.textColor = [UIColor whiteColor];
    _loginBtn.titleLabel.shadowColor = [UIColor whiteColor];
    [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [_loginBtn setTitle:@"登录" forState:UIControlStateHighlighted];
    _loginBtn.layer.cornerRadius = 2;
    
    [_loginBtn addTarget:self action:@selector(clickLogin:) forControlEvents:UIControlEventTouchUpInside];
    [_loginBtn addTarget:self action:@selector(loginTouchdown:) forControlEvents:UIControlEventTouchDown];
    [_loginBtn addTarget:self action:@selector(loginTouchUpoutSide:) forControlEvents:UIControlEventTouchUpOutside];
    
    
    if(IS480)
    {
        backView.image = [UIImage imageNamed:@"loading960"];
        [self.view addSubview:backView];
        _loginBtn.frame = CGRectMake(20, ScreenHeight - 60, ScreenWidth -40, 44);
        [self createUser:CGRectMake(20, 185, ScreenWidth -40, 44)];
        
        [self createPass:CGRectMake(20, 236, ScreenWidth -40, 44)];
    }
    else if (IS568)
    {
        backView.image = [UIImage imageNamed:@"loading1136"];
        [self.view addSubview:backView];
        _loginBtn.frame = CGRectMake(20, ScreenHeight - 60, ScreenWidth -40, 44);
        [self createUser:CGRectMake(20, 195, ScreenWidth -40, 44)];
        
        [self createPass:CGRectMake(20, 246, ScreenWidth -40, 44)];
    }
    else if (IS667) {
        backView.image = [UIImage imageNamed:@"loading1334"];
        [self.view addSubview:backView];
        
        _loginBtn.frame = CGRectMake(28, ScreenHeight - 60, ScreenWidth -56, 44);
        [self createUser:CGRectMake(28, 270, 318, 44)];
        [self createPass:CGRectMake(28, 321, 318, 44)];
    }
    else if(IS736)
    {
        backView.image = [UIImage imageNamed:@"loading2208"];
        [self.view addSubview:backView];
        
        _loginBtn.frame = CGRectMake(28, ScreenHeight - 60, ScreenWidth -56, 44);
        [self createUser:CGRectMake(20, 302, ScreenWidth -40, 44)];
        
        [self createPass:CGRectMake(20, 360, ScreenWidth -40, 44)];
    }
    
    
    [self.view addSubview:_loginBtn];
    

    
    
}

- (void)createUser:(CGRect )rect
{
    
    _userView = [[UIView alloc]init];
    _userView.frame = rect;
    _userView.backgroundColor = [UIColor whiteColor];
    _userView.layer.cornerRadius = 2;
    
    [self.view addSubview:_userView];
    
    UIImageView *userImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"loginUser"]];
    userImage.frame = CGRectMake(7, 10, 24, 24);
    [_userView addSubview:userImage];
    
    
    _userTF = [[UITextField alloc]initWithFrame:CGRectMake(38, 7, _userView.sizeWidth-43, 30)];
    _userTF.font = FontSize(14);
    _userTF.borderStyle = UITextBorderStyleNone;
    _userTF.returnKeyType = UIReturnKeyNext;
    _userTF.keyboardType = UIKeyboardTypePhonePad;
    _userTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _userTF.delegate = self;
    [_userView addSubview:_userTF];
    
    
}
- (void)createPass:(CGRect )rect
{
    _passView = [[UIView alloc]init];
    _passView.frame = rect;
    _passView.backgroundColor = [UIColor whiteColor];
    _passView.layer.cornerRadius = 2;
    [self.view addSubview:_passView];
    
    
    UIImageView *userImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"loginPass"]];
    userImage.frame = CGRectMake(7, 10, 24, 24);
    [_passView addSubview:userImage];
    
    
    _passwordTF = [[UITextField alloc]initWithFrame:CGRectMake(38, 7, _passView.sizeWidth-43, 30)];
    _passwordTF.font = FontSize(14);
    _passwordTF.borderStyle = UITextBorderStyleNone;
    _passwordTF.returnKeyType = UIReturnKeyGo;
    _passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _passwordTF.delegate = self;
    _passwordTF.secureTextEntry = YES;
    
    [_passView addSubview:_passwordTF];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == _userTF) {
        [_passwordTF becomeFirstResponder];
    }
    else
    {
        [self clickLogin:nil];
    }
    
    
    return YES;
}


- (IBAction)loginTouchdown:(UIButton *)sender {
    [self.loginBtn setBackgroundColor:RGB(39, 104, 204, 1)];
}

- (IBAction)loginTouchUpoutSide:(UIButton *)sender {
     [self.loginBtn setBackgroundColor:RGB(71,133,229, 1)];
}

- (IBAction)clickLogin:(UIButton *)sender {
    [self.view endEditing:YES];
    
     [self.loginBtn setBackgroundColor:RGB(71,133,229, 1)];
    
    if ([_userTF.text isEqualToString:@""]||[_passwordTF.text isEqualToString:@""]) {
        FailedShow(@"请完善账号密码");
        return;
    }
    
    UserBuilder *user = [[UserBuilder alloc] init];
    
    user.name = _userTF.text;
    user.pass = _passwordTF.text;

    [[UserLogic shareInstance]loginWithUser:user.build];
    SetNSUserDefaults(@"UserName", _userTF.text);
    HUDShow(@"");
    
}
- (void)loginError:(NSNotification *)noti
{
    HUDRemove;
    if ([noti.object isKindOfClass:[ErrorModel class]])
    {
        ErrorModel *model = noti.object;
        [UIViewCategory FailShowWith:model];
    }
    else
    {
        
    }
}
- (IBAction)clickClose:(UIButton *)sender {
    [self.view endEditing:YES];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"gotoMP" object:nil];
//    [self dismissViewControllerAnimated:YES completion:nil];
    

}

@end
